package com.purong.purong.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.Toast;

import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.activity.LoginActivity;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.net.ResponeHandle;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.encrypt.MD5;
import com.yao.sdk.net.NetWork;
import com.yao.sdk.net.NetWorkListener;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/10.
 */
public class NetFragment extends Fragment implements NetWorkListener {
    @Override
    public void onResponse(final ResponseInfo responseInfo) {
        if (responseInfo.isOk&&responseInfo.content!=null) {

//            responseInfo.content = new Encrypt().toDecryption(responseInfo.content);
//            BaseBean bean = JSON.parseObject(responseInfo.content, BaseBean.class);
            responseInfo.resultCode= ResponeHandle.isSuccess(getActivity(), responseInfo);
            if (responseInfo.resultCode.equals("0")) {
                responseInfo.isRequestOk = true;
            }
            else if (responseInfo.resultCode.equals("C0002")) {

                reInit(new ReInitListener(responseInfo.url,responseInfo.params));//若返回接口过期，重新初始化或登录获取token
                responseInfo.isOk=false;
            }
        }else if(!responseInfo.isOk) {
            Log.e("zwj","....请求服务器失败_"+responseInfo.url);
            Log.e("zwj","....请求服务器失败_"+responseInfo.error.errorInfo);
//            if (getActivity()!=null)
//            Toast.makeText(getActivity(),"请求服务器失败",Toast.LENGTH_SHORT).show();
        }
    }
    public boolean request(final String url, final Map<String, String> params, String token) {
        if (!CommonUtil.isNetConnected(getActivity())){
            if (getActivity()!=null)
            Toast.makeText(getActivity(),"网络未连接",Toast.LENGTH_SHORT).show();
            return false;
        }

        NetWork.request(this, url, params,token);


        return true;
    }
    // 网络请求
    public boolean request(final String url, final Map<String, String> params, String token,NetWorkListener listener) {
        if (!CommonUtil.isNetConnected(getActivity())){
            if (getActivity()!=null)
            Toast.makeText(getActivity(),"网络未连接",Toast.LENGTH_SHORT).show();
            return false;
        }


        NetWork.request(listener, url, params,token);


        return true;
    }
    public boolean requestNoToast(final String url, final Map<String, String> params, String token) {
        if (!CommonUtil.isNetConnected(getActivity())){
            return false;
        }

        NetWork.request(this, url, params,token);


        return true;
    }
    // 网络请求
    public boolean requestNoToast(final String url, final Map<String, String> params, String token,NetWorkListener listener) {
        if (!CommonUtil.isNetConnected(getActivity())){
            return false;
        }


        NetWork.request(listener, url, params,token);


        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        NetWork.cancelRequest(this.getActivity());
    }
    public void cancelRequest(){
        NetWork.cancelRequest(this.getActivity());
    }
    public void reInit(NetWorkListener listener){
//        Log.e("zwj",".............reinit");
//        if (PuRongApp.getLoginInfo(getActivity())!=null){
//            Log.e("zwj",".............reinit login");
//            Map params =new LinkedHashMap();
//            params.put("user_phone", PuRongApp.getLoginInfo(getActivity()).getPhone());
//            params.put("user_pwd",PuRongApp.getLoginInfo(getActivity()).getPwd());
//            if (listener==null){
//                request(Contant.LOGIN, params,  SharePreUtil.getInstance(getActivity()).getToken());
//            }else {
//                request(Contant.LOGIN, params, SharePreUtil.getInstance(getActivity()).getToken(), listener);
//            }
//        }else {
        if (PuRongApp.getLoginInfo(getActivity())!=null) {
            LoginBean.remove(getActivity());
        }
        Log.e("zwj",".............reinit initial");
        Map params =new LinkedHashMap();
        Log.e("zwj","getIMEI="+ MD5.getMD5Str(CommonUtil.getIMEI(getActivity())));
        params.put("device_no", MD5.getMD5Str(CommonUtil.getIMEI(getActivity())));
        params.put("os","1");
        params.put("version",CommonUtil.getVersionCode(getActivity()));
        if (listener==null){
            request(Contant.INITIAL, params, null);
        }else {
            request(Contant.INITIAL, params,null,listener);
        }
//        }

    }
    class ReInitListener implements NetWorkListener {
        String url;
        public Map<String, String> params=new LinkedHashMap<>();
        public ReInitListener(String url,Map params){
            this.url=url;
            this.params.putAll(params);
        }
        @Override
        public void onResponse(ResponseInfo reInitInfo) {

//            reInitInfo.content = new Encrypt().toDecryption(reInitInfo.content);
//            BaseBean bean = JSON.parseObject(reInitInfo.content, BaseBean.class);
            String code = ResponeHandle.isSuccess(getActivity(), reInitInfo);
            if (code.equals("0")) {
                Log.e("zwj",".............reinit_Respons success");
                Log.e("zwj",".............reinit_url "+url);
                request(url,params, SharePreUtil.getInstance(getActivity()).getToken());
            }else{
                Log.e("zwj",".............reinit_Respons faile");
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    Log.e("zwj",".............reinit_Respons faile logout");
                    LoginBean.remove(getActivity());
                    Intent intent=new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
            }
        }
    }
}
